#! /bin/sh



execcmd=
machineip=
loginuser=root
loginpassword=6JyjvPSjpS#FXCX6Ud



####
#### Usage: sshcmd cmd testmathineip [password] [user]
#####     password:default huawei 
#####     user:root
#####     eg: sshcmd ls 128.5.120.78 huawei root
### Value of the sshcmd return:
### return 13: it means can not connect the mathine or password is wrong
### return 0: command execute successfully
### return others: command execute wrongly
####


sshcmd_comm()
{

	cmd="$1"
	testip=$2
	password=${3-opensuse}
	user=${4-root}
	timeout=180

	if [ "$1x" = "x" ]; then
		echo "ssh_password cmd targetip [password] [user]"
		return 1
	fi

	 if [ "x${testip}" = "x" ]; then
                testip=$testmachine
        fi

	### change " to \"
	cmd=${cmd//\"/\\\"}
	### change $ to \$
	cmd=${cmd//\$/\\\$}

        if [ "x$testip" = "x" -o "x${cmd}" = "x" ];then
                echo "isup  time testmathine [password] [user]"
                exit 1
        fi
	expect <<-END1
		## set infinite timeout, because some commands maybe execute long time. 
		set timeout -1
	
		## remotly exectue command
		spawn ssh -o "ConnectTimeout ${timeout}" ${user}@${testip} "${cmd}"

		expect {

			#first connect, no public key in ~/.ssh/known_hosts

			"Are you sure you want to continue connecting (yes/no)?" {

				send "yes\r"

				expect -re "\[P|p]assword:"

				send "${password}\r"

			}

			## already has public key in ~/.ssh/known_hosts
			-re "\[P|p]assword:" {

				send "${password}\r"
          		}

			## connect target mathine time out
			timeout {
				send_user "connection to $targetip timed out: \$expect_out(buffer)\n"
				exit 13
        		}

			## Do not need input password. Becauese of ssh 
			eof {
				catch wait result
				#send_user  [lindex \$result 3]
				exit [lindex \$result 3] 
			}
       		}

		### We have input password,and the command may have been execute,except password is wrong or connctione is broken.
       		expect {
			## check exit status of the proccess of ssh 
		 	eof {
				catch wait result
				exit [lindex \$result 3] 
			}

			## Password is wrong!
	        	-re "\[P|p]assword:" {
				send_user "invalid password or account. \$expect_out(buffer)\n"
                		exit 13
        		}

			## timeout again
			timeout {
				send_user "connection to $targetip timed out : \$expect_out(buffer)\n"
				exit 13
			}

    	}
	
	END1

	return $?
}


while getopts "c:m:p:u:h" OPTIONS
do
        case $OPTIONS in
                c) execcmd="$OPTARG";;
                m) machineip="$OPTARG";;
                u) loginuser="$OPTARG";;
                p) loginpassword="$OPTARG";;
                \?) echo "ERROR - Invalid parameter"; echo "ERROR - Invalid parameter" >&2;usage;exit 1;;
                *) echo "ERROR - Invalid parameter"; echo "ERROR - Invalid parameter" >&2; usage;exit 1;;
        esac
done
usage()
{
        echo "Usage: sshcmd.sh -c "command" -m "machinetip" [-u login_user] [-p login_password]"
}

if [ "x$execcmd" = "x" -o "x$machineip" = "x" ];then
        usage
        exit 1
fi

sshcmd_comm "$execcmd" "$machineip" "$loginpassword" "$loginuser"

exit $?
